<template>
  <div class="find">
    <!-- 吸顶 <van-sticky> -->
    <van-sticky>
      <hmNavBar :showIcon="false" title="发现"></hmNavBar>
    </van-sticky>
    <!-- 下拉刷新 -->
    <van-pull-refresh v-model="loading" @refresh="refresh">
      <div class="find-content">
        <findCell title="面试技巧" @click.native="$router.push('/home/skill')">
        </findCell>
        <techincItem
          v-for="(item, index) in technicList"
          :key="index"
          :info="item"
        >
          <!-- 通过props传值子组件info -->
        </techincItem>

        <!-- 市场数据 -->
        <findCell title="市场数据"> </findCell>
        <!-- tag
          van-tag
          color:背景颜色
          text-color:文本颜色      
       -->
        <van-tag class="tag" color="#eceaea" text-color="#9f9eac"
          >{{ chartList.city }}
        </van-tag>
        <van-tag class="tag" color="#eceaea" text-color="#9f9eac"
          >{{ chartList.position }}
        </van-tag>
        <ul class="chart" ref="chart">
          <li
            class="chart-item"
            v-for="(item, index) in chartList.yearSalary"
            :key="index"
          >
            <!-- v-show="index < 3 || showAll" -->
            <div class="c1">{{ item.year.substr(0, 5) }}</div>
            <div class="c2">
              <div
                class="line"
                :style="{
                  width: (item.salary / +chartList.topSalary) * 100 + '%'
                }"
              >
                {{ item.salary }}
              </div>
            </div>
            <div class="c3">
              <div v-if="item.percent != undefined">
                <!-- 下降 -->
                <i class="iconfont" v-if="item.percent < 0"> &#xe64e; </i>
                <!-- 上升 -->
                <i v-else class="iconfont"> &#xe651; </i>
              </div>
            </div>
            <div class="c4">
              <span v-if="item.percent != undefined">{{ item.percent }}%</span>
            </div>
          </li>
        </ul>
        <div class="look-more" @click="move">
          <div v-if="!showAll">
            <span>展开更多</span> <i class="iconfont">&#xe652;</i>
          </div>
          <div v-else>
            <span> 收起 </span> <i class="iconfont r180">&#xe652;</i>
          </div>
        </div>

        <!-- 面经分享 -->
        <findCell
          title="面试分享"
          @click.native="$router.push('/home/shareList')"
        >
        </findCell>
        <div>
          <shareItem
            v-for="(item, index) in shareList"
            :key="index"
            :info="item"
          >
          </shareItem>
        </div>
      </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import findCell from './findCell'
import techincItem from './techincItem'
import { articlesTechnic, chartHot, articlesShare } from '@/api/find.js'
import shareItem from './shareItem'

export default {
  name: 'find',
  components: {
    findCell,
    techincItem,
    shareItem
  },
  data () {
    return {
      technicList: [],
      chartList: '',
      showAll: false,
      shareList: '',
      loading: false //下拉刷新v-model
    }
  },

  /* 调用api保存 */
  created () {
    this.refresh()
  },
  methods: {
    move () {
      /* 点击展开更多与收起时,对盒子进行了高度计算 */
      this.showAll = !this.showAll
      if (this.showAll) {
        //显示所有  ,让它的高度=正个盒子的高度
        this.$refs.chart.style.height = this.$refs.chart.scrollHeight + 'px'
      } else {
        /* 当收起时,让它的高度等于3个小子项的高度 */
        this.$refs.chart.style.height =
          (this.$refs.chart.scrollHeight / this.chartList.yearSalary.length) *
            3 +
          'px'
      }
    },
    async refresh () {
      const res = await articlesTechnic()
      this.technicList = res.data.data.list
      window.console.log(res)

      /* 调用api获取数据并保存 */
      const res2 = await chartHot()
      window.console.log('数据', res2)
      this.chartList = res2.data.data

      /* 在获取到chart数据后,给它定一个初始高度显示前三个 */
      this.$nextTick(() => {
        this.$refs.chart.style.height =
          (this.$refs.chart.scrollHeight / this.chartList.yearSalary.length) *
            3 +
          'px'
      })

      /* 面经分享数据 */
      const res3 = await articlesShare()
      this.shareList = res3.data.data.list
      window.console.log('面经分享', res3)
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.find {
  .find-content {
    padding: 20px @p15;
  }
  .tag {
    margin-right: 10px;
    padding: 4px 6px;
  }
  .chart {
    /*在chart盒子加上transition  */
    transition: height 1s;
    overflow: hidden;
    margin-top: 15px;
    .chart-item {
      display: flex;
      font-size: 14px;
      align-items: center;
      color: #545671;
      line-height: 20px;
      padding: 4px 0;

      .c1 {
        margin-right: 2px;
        width: 50px;
      }
      .c2 {
        margin-right: 2px;
        flex: 1;
        height: 14px;
        background: #ebdfdf;
        border-radius: 4px;

        .line {
          height: 14px;
          height: 100%;
          background: #fe6d67;
          border-radius: 4px;
          text-align: right;
          padding-right: 5px;
          font-size: 12px;
          line-height: 14px;
          color: #fff;
        }
      }
      .c3 {
        margin-right: 2px;
        width: 20px;
        color: #fe6d67;
      }
      .c4 {
        width: 40px;
      }
    }
  }
  .look-more {
    font-size: 14px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: center;
    color: #545671;
    line-height: 20px;
    letter-spacing: 0px;
    .r180 {
      display: inline-block;
      /* transform块级元素和部分行内元素 */
      transform: rotate(180deg);
    }
  }
}
</style>
